package com.mvc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.common.core.BaseController;
import com.common.utils.DateUtil;
import com.common.utils.JsonUtil;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.mvc.model.Car;
import com.mvc.request.FinalData;
import com.mvc.request.ReqCarAdd;
import com.mvc.request.ReqCarDelete;
import com.mvc.request.ReqCarPage;
import com.mvc.request.ReqCarQPage;
import com.mvc.request.ReturnData;
import com.mvc.response.CarUI;
import com.mvc.response.PageList;
import com.mvc.response.RspPage;
import com.mvc.service.CarService;
import com.mvc.service.impl.CarServiceImpl;

public class CarController extends BaseController {

	CarService carService = new CarServiceImpl();

	public void addView() {
		render("add.jsp");

	}
	public void apk() {
		render("apk.jsp");

	}
	
	public void search() {
		render("search.jsp");

	}

	public void searchCar() throws Exception
	{
		
		ReqCarQPage req = new ReqCarQPage();
		FinalData finalData = new FinalData();
		req.setFinalData(finalData);

        String content = getPara("address");
        if (StrKit.notBlank(content))
        {
            content = content.replaceAll("到","-");
            content = content.replaceAll("去","-");
            content = content.replaceAll("回","-");
            req.setAddress(content);
        }
        if (StrKit.notBlank(getPara("faTime")))
        {
        	req.setFaTime(DateUtil.formatString(getPara("faTime"), "yyyy-MM-dd HH:mm:ss"));
        }else {
        	req.setFaTime(new Date());
		}
		
		req.setType(getParaToInt("type", 2));
		req.setNowPage(getParaToInt("pageIndex",0)+1);
		req.setSizePerson(getPara("sizePerson","1"));
		req.setPageSize(10);
		

		ReturnData<RspPage<Car>> data = carService.querPage(req);
		PageList<CarUI>  pageList = new PageList<CarUI>();
		if(data.getCode() == 200)
		{
			List<Car> list = data.getData().getList();
			List<CarUI> liCarUIs = new ArrayList<CarUI>(list.size());
			for(Car car :list)
			{
				CarUI carUI = new CarUI();
	
				carUI.setContentBZ(car.getStr(Car.TableField.CONTENT));
				carUI.setEndAddress(car.getStr(Car.TableField.ENDADDRESS));
				carUI.setMoney(car.getStr(Car.TableField.MONEY));
				carUI.setPhone(car.getStr(Car.TableField.PHONE));
				carUI.setSizePerson(car.getInt(Car.TableField.SIZEPERSON)+"");
				carUI.setStartAddress(car.getStr(Car.TableField.STARTADDRESS));
				
				String type="";
				int size = car.getInt(Car.TableField.SIZEPERSON);
				if (car.getInt(Car.TableField.TYPE)==1)
				{
					type = "<span class=\"badge badge-success\">车主/"+size+"</span>";
				}else {
					type ="<span class=\"badge badge-error\">乘客/"+size+"</span>";
				}
				
				carUI.setType(type);
				Date createTime = car.getDate(Car.TableField.CREATETIME);
				carUI.setCreateTime(DateUtil.friendTime(createTime));
				
				Date faTime = car.getDate(Car.TableField.FATIME);
				
				 String time = "<font color=\"#FF4081\">" + DateUtil.toFormat_Y_M_D_H_M(faTime) + "</font>";
			        time += "&nbsp;<font color=\"#000000\">" + DateUtil.getDateDetail(faTime) + "</font>";
			        time += "&nbsp;<span class=\"badge badge-error\">发</span>";
			        
				carUI.setFaTime(time);
				liCarUIs.add(carUI);
			}
			pageList.setResults(data.getData().getTotalPage()*req.getPageSize());
			pageList.setRows(liCarUIs);
			pageList.setError("");
			pageList.setHasError(false);
		}
		

			
			renderJson(JsonUtil.toJson(pageList));
		
	}
	public void addCar() throws Exception
	{

		ReqCarAdd add  = new ReqCarAdd();
		if (add.getFinalData() == null)
		{
			FinalData finalData = new FinalData();
			finalData.setMac("02000000000037d252e043232cf199000644867408");
			finalData.setCode(100);
			finalData.setType("web");
			add.setFinalData(finalData);
		}
		
		
		add.setContent(getPara("content"));
		add.setType(getParaToInt("type"));
		add.setStartAddress(getPara("startAddress"));
		add.setEndAddress(getPara("endAddress"));
		add.setMoney(getPara("money"));
		add.setPhone(getPara("phone"));
		add.setSizePerson(getPara("size"));
		add.setFaTime(DateUtil.formatString(getPara("faTime"), "yyyy-MM-dd HH:mm:ss"));
		ReturnData<Boolean> data = carService.add(add);
		renderJson(JsonUtil.toJson(data));
	}
	
	
	public void add() {

		ReqCarAdd req = getRequestObject(ReqCarAdd.class,"yyyy-MM-dd HH:mm:ss");
		ReturnData<Boolean> data = carService.add(req);
		renderJson(JsonKit.toJson(data));
	}

	public void delete() {
		ReqCarDelete req = getRequestObject(ReqCarDelete.class);
		ReturnData<Boolean> data = carService.delete(req);
		renderJson(JsonKit.toJson(data));
	}

	// 获取最新列表-找车列表-找人列表-我发布的列表
	public	void queryPage() {
		ReqCarPage req = getRequestObject(ReqCarPage.class);
		ReturnData<RspPage<Car>> data = carService.queryPage(req);
		renderJson(JsonKit.toJson(data));
	}

	// 获取条件搜索列表
	public	void queryTermPage() {
		ReqCarQPage req = getRequestObject(ReqCarQPage.class,"yyyy-MM-dd HH:mm:ss");
		ReturnData<RspPage<Car>> data = carService.querPage(req);
		renderJson(JsonKit.toJson(data));
	}
}
